package com.sskj.login.presenter;

import com.lzy.okgo.model.Response;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;

import com.sskj.lib.http.JsonCallBack;
import com.sskj.login.http.HttpConfig;
import com.sskj.login.ui.activity.RegisterActivity;


public class RegisterActivityPresenter extends BasePresenter<RegisterActivity> {

    public void sendCode(String mcode,String account) {
        if (account.contains("@")) {
            sendEmail(account);
        } else {
            sendSms(mcode,account, "1");
        }
    }

    /**
     * 发送验证码（注册）
     *
     * @param mobile 手机号
     * @param type   1注册 （2 重置 3 安全验证 4 资金密码设置 5 提币）
     * @return
     */
    public void sendSms(String mcode,String mobile, String type) {

        httpService.sendSms(mcode,mobile, type).execute(callback);
    }

    JsonCallBack<HttpData> callback = new JsonCallBack<HttpData>(this) {
        @Override
        public void onSuccess(Response<HttpData> response) {
            HttpData httpData = response.body();
            if (httpData.getStatus() == HttpConfig.OK) {
                mView.sendCheckCodeSuccess();
            } else {
                ToastUtil.showShort(httpData.getMsg());
                mView.sendCheckCodeFail();
            }
        }

        @Override
        public void onError(Response<HttpData> response) {
            mView.sendCheckCodeFail();
            super.onError(response);
        }
    };

    /**
     * 发送邮件
     *
     * @param email 邮箱号
     * @return
     */
    public void sendEmail(String email) {
        httpService.sendEmail(email, null).execute(callback);
    }


    public void checkCode(String account, String code) {

        httpService.checkCode(account, code)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        ToastUtil.showShort(httpData.getMsg());
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.checkCodeSuccess();
                        }
                    }
                });
    }


    /**
     * 手机号注册
     *
     * @param mcode  区号
     * @param mobile 手机号/邮箱号
     * @param code   验证码
     * @param opwd   密码
     * @param opwd1  重复密码
     * @param tjuser 推荐码/邀请码
     * @return
     */
    public void register(String mcode, String mobile, String code, String opwd, String opwd1, String tjuser) {
        httpService.register(mcode, mobile, code, opwd, opwd1, tjuser)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.registerSuccess();
                        }
                    }
                });
    }

    /**
     * 邮箱注册
     *
     * @param mobile 手机号/邮箱号
     * @param code   验证码
     * @param opwd   密码
     * @param opwd1  重复密码
     * @param tjuser 推荐码/邀请码
     * @return
     */
    public void emailRegister(String mobile, String code, String opwd, String opwd1, String tjuser) {
        httpService.register("", mobile, code, opwd, opwd1, tjuser).execute(new JsonCallBack<HttpData>(this) {
            @Override
            public void onSuccess(Response<HttpData> response) {
                HttpData httpData = response.body();
                if (httpData.getStatus() == HttpConfig.OK) {
                    mView.registerSuccess();
                }
            }
        });
    }
}
